package com.cpit.platform.common.dao;

import org.springframework.util.StringUtils;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * 简单条件表达式 
 */
public class SimpleExpression implements Criterion {
	private String fieldName;       //属性名  
    private Object value;           //属性对应值
    private Operator operator;      //计算符 
    private MatchMode matchMode;    //like匹配方式
    private Object minValue; //属性对应最小值
    private Object maxValue;//属性对应最大值
    private List valueList;//属性对应最大值

    protected SimpleExpression(String fieldName, Operator operator) {
        this.fieldName = fieldName;
        this.operator = operator;
    }

    protected SimpleExpression(String fieldName, Object value, Operator operator) {  
        this.fieldName = fieldName;  
        this.value = value;  
        this.operator = operator;  
    }  
    
    protected SimpleExpression(String fieldName, Object value, MatchMode matchMode, Operator operator) {  
        this.fieldName = fieldName;  
        this.value = value;  
        this.operator = operator; 
        this.matchMode = matchMode;
    }
    protected SimpleExpression(String fieldName, Object minValue,Object maxValue,Operator operator) {
        this.fieldName = fieldName;
        this.minValue = minValue;
        this.operator = operator;
        this.maxValue = maxValue;
    }
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public Predicate toPredicate(Root<?> root, CriteriaQuery<?> query,  
            CriteriaBuilder builder) {  
        Path expression = Criterion.getPath(root,fieldName);
        if (expression == null){
            return null;
        }
        return Criterion.toSimplePredicate(operator,builder, expression,
                value,minValue,maxValue, matchMode) ;
    }

    public String getFieldName() {
        return fieldName;
    }

    public void setFieldName(String fieldName) {
        this.fieldName = fieldName;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }

    public Operator getOperator() {
        return operator;
    }

    public void setOperator(Operator operator) {
        this.operator = operator;
    }

    public MatchMode getMatchMode() {
        return matchMode;
    }

    public void setMatchMode(MatchMode matchMode) {
        this.matchMode = matchMode;
    }

    public Object getMinValue() {
        return minValue;
    }

    public void setMinValue(Object minValue) {
        this.minValue = minValue;
    }

    public Object getMaxValue() {
        return maxValue;
    }

    public void setMaxValue(Object maxValue) {
        this.maxValue = maxValue;
    }

    public List getValueList() {
        return valueList;
    }

    public void setValueList(List valueList) {
        this.valueList = valueList;
    }
}
